import {fromJS, List, Map, Seq} from 'immutable';
import * as Cursor from 'immutable/contrib/cursor';
import * as folderStore from '../src/core';

function setConnectionState(state, connectionState, connected) {
  return state.set('connection', Map({
    state: connectionState,
    connected
  }));
}

function setState(state, newState) {
  return state.merge(newState);
}

function vote(state, entry) {
  const currentRound = state.getIn(['vote', 'round']);
  const currentPair = state.getIn(['vote', 'pair']);
  if (currentPair && currentPair.includes(entry)) {
    return state.set('myVote', Map({
      round: currentRound,
      entry
    }));
  } else {
    return state;
  }
}

function resetVote(state) {
  const votedForRound = state.getIn(['myVote', 'round']);
  const currentRound = state.getIn(['vote', 'round']);
  if (votedForRound !== currentRound) {
    return state.remove('myVote');
  } else {
    return state;
  }
}

function initState(state){
    return folderStore.setFolderTree(state, window.rootFolder)
}
function setCurFolderId(state, folderId){
    return state.set("curFolderId", folderId); 
}
function setFolderChildren(folderTreeState, folderId, folderChildren){
    return folderStore.setFolderChildren(folderTreeState, folderId, fromJS(folderChildren));    
}
function moveToNode(state, node, toId) {
    return state.update("folderTree",
        folderTree => {
            var folderTree1 = folderStore.removeNode(folderTree, node.get("id"));
            return folderStore.addNode(folderTree1, toId, node);
        })    
}

function copyToNode(state, node, toId) {
    return state.update("folderTree",
        folderTree => {
            return folderStore.addNode(folderTree, toId, node);
        })
}

function removeNode(state, nodeId){
    return state.update("folderTree",
        folderTree => {
            return folderStore.removeNode(folderTree, nodeId);
        })
}

function addNode(state, node, toId){
    return state.update("folderTree",
        folderTree => {
            return folderStore.addNode(folderTree, toId, node);
        })
}


function updateNode(state, nodeId, node){
    return state.update("folderTree",
        folderTree => {
            return folderStore.updateNode(folderTree, nodeId, node);
        })
}

export default function(state = Map(), action) {   
    switch (action.type) {
        case '@@redux/INIT':
            return initState(state);
        case 'SET_CUR_FOLDER_ID':
            return setCurFolderId(state, action.folderId);
        case 'SET_FOLDER_CHILDREN':
            return state.update("folderTree", folderTreeState => setFolderChildren(folderTreeState, action.folderId, action.folderChildren));
        case 'MOVE_TO':
            return moveToNode(state, action.node, action.toId);
        case 'COPY_TO':
            return copyToNode(state, action.node, action.toId);
        case 'REMOVE':
            return removeNode(state, action.nodeId);       
        case 'ADD':
            return addNode(state, action.node, action.toId);    
        case 'UPDATE':
            return updateNode(state, action.nodeId, action.node); 

    }
    return state;
}
